﻿using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.DepartMentes;
using Management.Systeam.Read.Apis.Application.Command.Rolees;
using MediatR;
using System.Text;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.DepartMentes
{
    public class ShowDepartMentCommandHandler : IRequestHandler<ShowDepartMentCommand, ApiResult<List<DepartMents>>>
    {
        IBaseRepository<DepartMents> DepartMents;

        public ShowDepartMentCommandHandler(IBaseRepository<DepartMents> DepartMents)
        {
            this.DepartMents = DepartMents;
        }

        public async Task<ApiResult<List<DepartMents>>> Handle(ShowDepartMentCommand request, CancellationToken cancellationToken)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECT * FROM DepartMents");
            sb.Append(" WHERE IsDel LIKE '%" + 0 + "%'");
            if (request.Department_Name != null)
            {
                sb.Append(" AND Department_Name LIKE '%" + request.Department_Name + "%'");
                //Sql += " WHERE Department_Name LIKE '%"+request.Department_Name+"%'";
            }
            if (request.Parent_Department != null)
            {
                sb.Append(" AND Parent_Department LIKE '%" + request.Parent_Department + "%'");
                //Sql += " AND Parent_Department LIKE '%"+request.Parent_Department+"%'";
            }
            sb.Append(" ORDER BY Department_Id DESC");
            var list = await DepartMents.QueryAsync(sb.ToString());

            return new ApiResult<List<DepartMents>>
            {
                Code = ApiEnum.success,
                Message = "获取用户列表成功",
                Data = list.ToList()
            };
        }
    }
}
